﻿using Demo_Chart图表样式.Service;
using Microsoft.Research.DynamicDataDisplay;
using Microsoft.Research.DynamicDataDisplay.DataSources;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace Demo_Chart图表样式.View
{
    /// <summary>
    /// MyUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class MyUserControl : UserControl
    {
        private int nclickTimes = 1;

        public MyUserControl()
        {
            InitializeComponent();
            DataContext = ViewModel.ViewModel.GetInstance().m_Model_MainWindow;
            D3Init();
            DiyInitSendTimer();
        }

        private void button_ClearChart_Click(object sender, RoutedEventArgs e) => ClearChartLine1();
        private void button_ClearChart2_Click(object sender, RoutedEventArgs e) => ClearChartPoint();

        private void Button_AddLine_Click(object sender, RoutedEventArgs e) => ViewModel.ViewModel.GetInstance().m_Model_MainWindow.ADD(0);
        private void Button_AddLine1_Click(object sender, RoutedEventArgs e) => ViewModel.ViewModel.GetInstance().m_Model_MainWindow.ADD(1);
        private void Button_AddLine2_Click(object sender, RoutedEventArgs e)
        {
            nclickTimes++;
            if (nclickTimes % 2 == 0)
            {
                StartSend(200);
            }
            else
            {
                StopSend();
            }
        }

        #region 标准化 - 1个图表控件 多条线（不同颜色）

        private void D3Init() => Line1.Children.Remove(Line1.DefaultContextMenu);//去除第三方控件自带的右键菜单

        private ObservableCollection<CsSQLite> LL1 = new ObservableCollection<CsSQLite>();

        public void AddChartLine1(ObservableCollection<CsSQLite> pL, bool bIsClear)//pL传入线条点的资源，线条颜色，线条名字
        {
            if (LL1.Count != 0 && bIsClear == true)
                ClearChartLine1();

            List<LineGraph> m_LineGraph1 = new List<LineGraph>();
            int nLineCount = pL.Count;
            for (int i = 0; i < nLineCount; i++)
            {
                m_LineGraph1.Add(new LineGraph());
                m_LineGraph1[i] = Line1.AddLineGraph(pL[i].LinePointXY, pL[i].LineColor, 1, pL[i].LineName);
            }

            LL1.Add(new CsSQLite() { Line = m_LineGraph1 });//用ObservableCollection<CsSQLite> 来管理多条线条，可以指定删除某一条或集中管理

            Line1.Viewport.FitToView();//自适应图表
            //Line1.Legend.Visibility = Visibility.Hidden;//隐藏线条名
        }

        private void ClearChartLine1()
        {
            int nLLCount = LL1.Count;
            for (int i = 0; i < nLLCount; i++)
            {
                int nLineCount = LL1[i].Line.Count;
                for (int j = 0; j < nLineCount; j++)
                    Line1.Children.Remove(LL1[i].Line[j]);
            }

            LL1.Clear();
        }


        #endregion

        #region 图表数据 增/删

        private int nTime = 0;
        private ObservableDataSource<Point> dataSource1 = null;
        private LineGraph graphSin1 = new LineGraph();

        private void LineInit()
        {
            dataSource1 = new ObservableDataSource<Point>();
            graphSin1 = Line1.AddLineGraph(dataSource1, Colors.Blue, 1, "Test");
            Line1.Viewport.FitToView();//自适应图表
        }

        public void AddChartPoint(double dbTempertrue)
        {
            if (dataSource1 == null)
                LineInit();
            Point point1 = new Point(nTime++, dbTempertrue);//x,y
            dataSource1.AppendAsync(this.Dispatcher, point1);

        }

        private void ClearChartPoint()
        {
            nTime = 0;
            Line1.Children.Remove(graphSin1);
            dataSource1 = null;
        }

        #region 后台线程

        public DispatcherTimer DiyInitDispatcherTimer = new DispatcherTimer();   /* 自动发送定时器 */

        public void DiyInitSendTimer()//需要初始化
        {
            DiyInitDispatcherTimer.IsEnabled = false;
            DiyInitDispatcherTimer.Tick += DiyInitSendDispatcherTimer_Tick;//定义自动发送事件
        }

        public void StartSend(int interval)//开始发送单位毫秒ms
        {
            DiyInitDispatcherTimer.IsEnabled = true;
            DiyInitDispatcherTimer.Interval = TimeSpan.FromMilliseconds(interval);
            DiyInitDispatcherTimer.Start();
        }

        public void StopSend()//停止发送
        {
            DiyInitDispatcherTimer.IsEnabled = false;
            DiyInitDispatcherTimer.Stop();


        }

        public void DiyInitSendDispatcherTimer_Tick(object sender, EventArgs e)//自动发送事件
        {
            Random rd = new Random();
            int nData = rd.Next(-100, 100);
            System.Windows.Application.Current.Dispatcher.Invoke((Action)delegate ()
            {
                AddChartPoint(nData);
            });
        }

        #endregion


        #endregion


    }
}
